package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.AutoCommunicateDTO;
import com.xbongbong.paas.pojo.dto.CallAppFiltersDTO;
import com.xbongbong.paas.pojo.dto.CallCallLogListDTO;
import com.xbongbong.paas.pojo.dto.CallCallLogSaveDTO;
import com.xbongbong.paas.pojo.dto.CallCenterLoginDTO;
import com.xbongbong.paas.pojo.dto.CallCenterModuleDTO;
import com.xbongbong.paas.pojo.dto.CallMobileCallDTO;
import com.xbongbong.paas.pojo.dto.CallMobileCodeSendDTO;
import com.xbongbong.paas.pojo.dto.CallMobileCodeVerifyDTO;
import com.xbongbong.paas.pojo.vo.AutoCommunicateVO;
import com.xbongbong.paas.pojo.vo.CallAppFiltersVO;
import com.xbongbong.paas.pojo.vo.CallCallLogSaveVO;
import com.xbongbong.paas.pojo.vo.CallCenterLoginVO;
import com.xbongbong.paas.pojo.vo.CallCenterModuleVO;
import com.xbongbong.paas.pojo.vo.CallLogListVO;
import com.xbongbong.paas.pojo.vo.CallMobileCallVO;
import com.xbongbong.paas.pojo.vo.CallMobileCodeSendVO;
import com.xbongbong.paas.pojo.vo.CallMobileCodeVerifyVO;
import com.xbongbong.paas.service.CallCenterCallLogService;
import com.xbongbong.paas.service.CallCenterCommonService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @version v1.0
 * @description: 移动端呼叫中心相关接口
 * @date 2019/2/18 9:33
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/callCenter")
public class MobileCallCenterController {

    @Resource
    private CallCenterCommonService callCenterCommonService;
    @Resource
    private CallCenterCallLogService callCenterCallLogService;

    /**
     * 获取开启的话务中心平台（一家公司只能开启一个平台）
     *
     * @return 开启的话务中心列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/module", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String module(@RequestBody @Valid CallCenterModuleDTO callCenterModuleDTO, BindingResult br) {
        XbbResponse<CallCenterModuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallCenterModuleVO callCenterModuleVO = callCenterCommonService.module(callCenterModuleDTO);
                response = new XbbResponse<>(callCenterModuleVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 移动端获取呼出号码（呼出线路）
     * @return 移动端获取呼出号码（呼出线路）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/call", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileCall(@RequestBody @Valid CallMobileCallDTO callMobileCallDTO, BindingResult br) {
        XbbResponse<CallMobileCallVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.mobileCall(callMobileCallDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取移动端上的话务中心的筛选栏项目（度言）
     * @return 获取移动端上的话务中心的筛选栏项目（度言）
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/app/filters", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workOrderFilterSet(@RequestBody @Valid CallAppFiltersDTO callAppFiltersDTO, BindingResult br) {
        XbbResponse<CallAppFiltersVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.workOrderFilterSet(callAppFiltersDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存通话记录
     *
     * @return 保存通话记录
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callLog/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callLogSave(@RequestBody @Valid CallCallLogSaveDTO callCallLogSaveDTO, BindingResult br) {
        XbbResponse<CallCallLogSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCallLogService.callLogSave(callCallLogSaveDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取手机验证码
     *
     * @return 获取手机验证码
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/mobileCode/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileCodeSend(@RequestBody @Valid CallMobileCodeSendDTO callMobileCodeSendDTO, BindingResult br) {
        XbbResponse<CallMobileCodeSendVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.mobileCodeSend(callMobileCodeSendDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 验证手机验证码
     *
     * @return 验证手机验证码(帮助用户完成注册和登录的操作)
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/mobileCode/verify", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String mobileCodeVerify(@RequestBody @Valid CallMobileCodeVerifyDTO callMobileCodeVerifyDTO, BindingResult br) {
        XbbResponse<CallMobileCodeVerifyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.mobileCodeVerify(callMobileCodeVerifyDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 一键登录
     *
     * @return 用户一键登录使用的token或提醒用户进行注册
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String login(@RequestBody @Valid CallCenterLoginDTO callCenterLoginDTO, BindingResult br) {
        XbbResponse<CallCenterLoginVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = this.callCenterCommonService.login(callCenterLoginDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 通话记录列表
     *
     * @return 通话记录列表
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/callLog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String callLogList(@RequestBody @Valid CallCallLogListDTO callCallLogListDTO, BindingResult br) {
        XbbResponse<CallLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CallLogListVO callLogListVO = this.callCenterCallLogService.callLogList(callCallLogListDTO);
                response = new XbbResponse<>(callLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 自动添加跟进记录
     *
     * @return 自动添加跟进记录
     * @author youli.chen
     * @date 2019/2/12 14:34
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/autoCommunicate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String autoCommunicate(@RequestBody @Valid AutoCommunicateDTO autoCommunicateDTO, BindingResult br) {
        XbbResponse<AutoCommunicateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AutoCommunicateVO autoCommunicateVO = this.callCenterCommonService.autoCommunicate(autoCommunicateDTO);
                response = new XbbResponse<>(autoCommunicateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
